/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.dusin.web.util;

import java.sql.Timestamp;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import org.dusin.commonutil.DateUtil;

/**
 *
 * @author tengyu
 */
public class TheUtil {

    static public String getPointSelectCondition(HttpServletRequest request) {
        StringBuilder sb = new StringBuilder();
        String pointName = request.getParameter("name");
        if (pointName != null && !"".equals(pointName.trim())) {
            sb.append("and name like '%").append(pointName.trim()).append("%'");
        }

        String pointTypeIdStr = request.getParameter("pointTypeId");
        if (CommonWebUtil.validConditionParam(pointTypeIdStr)) {
            sb.append(" and pointTypeId=").append(pointTypeIdStr);
        }

        String pointType2IdStr = request.getParameter("pointType2Id");
        if (CommonWebUtil.validConditionParam(pointType2IdStr)) {
            sb.append(" and pointType2Id=").append(pointType2IdStr);
        }

        String voltageLevelIdStr = request.getParameter("voltageLevelId");
        if (CommonWebUtil.validConditionParam(voltageLevelIdStr)) {
            sb.append(" and voltageLevelId=").append(voltageLevelIdStr);
        }

        String reportStatusStr = request.getParameter("reportStatus");
        sb.append(CommonWebUtil.getEqualConditionStr("status/10%10", reportStatusStr));

        String startStatusStr = request.getParameter("startStatus");
        sb.append(CommonWebUtil.getEqualConditionStr("status%10", startStatusStr));

        String rangeStr = request.getParameter("range");
        sb.append(CommonWebUtil.getRangeConditionStr(rangeStr));

        String haveStr = request.getParameter("have");
        sb.append(getHaveConditionStr(haveStr));

        return sb.toString();
    }

    static String getHaveConditionStr(String have) {
        if (CommonWebUtil.validConditionParam(have)) {
            if ("1".equals(have)) {    //表示无数据
                return " and pointId is null";
            } else {
                return " and pointId is not null";  //有数据
            }
        } else {
            return "";
        }
    }

    static void formatListForExcel(List<Map<String, Object>> list) {

        for (Map<String, Object> map : list) {
            Timestamp minVoltageTime = (Timestamp) (map.get("minVoltageTime"));
            if (minVoltageTime != null) {
                map.put("minVoltageTime", DateUtil.get(minVoltageTime));
            }
            Timestamp maxVoltageTime = (Timestamp) (map.get("maxVoltageTime"));
            if (maxVoltageTime != null) {
                map.put("maxVoltageTime", DateUtil.get(maxVoltageTime));
            }             
        }
    }
    
    public static int getPointStatusCode(String startStatus,String reportStatus){
        int status = 0;
        if("启用".equals(startStatus)){
            status =1;
        }
        if("上报".equals(reportStatus)){
            status +=10;
        }
        return status;
    }
    
    public static String getReportStr(int reportStatus){
        if(reportStatus==1){
            return "上报";
        }else if(reportStatus==0){
            return "观测";
        }else{
            return "全部";
        }        
    }
    
    public static String getRangeStr(int range){
        if(range==1){
            return "子口径";
        }else if(range == 0){
            return "母口径";
        }else{
            return "全口径";
        }
    }
    
}
